package com.hk.service.sys.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.internal.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.ConstantUser;
import com.hk.commons.HqlFilter;
import com.hk.commons.LongId;
import com.hk.commons.entity.Parameter;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.entity.sys.SysUserRoles;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.order.IParameter;
import com.hk.service.sys.ISysUser;
import com.hk.service.sys.ISysUserRoles;

@Service
public class SysUserRolesImpl extends BaseServiceImpl<SysUserRoles> implements
		ISysUserRoles {

	private Logger logger = LoggerFactory.getLogger(SysUserRolesImpl.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private IParameter paraService;

	@Autowired
	private ISysUser userService;

	@Override
	public List<SysUserRoles> getByUserAccount(String account) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("t.account", account);
		return findByFilter(hqlFilter);
	}

	@Override
	public List<SysUserRoles> getByRoleId(Long roleId) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("t.roleId", roleId);
		return findByFilter(hqlFilter);
	}

	@Override
	public SysUserRoles findById(Long id) {
		return getById(id);
	}

	@Override
	public SysUserRoles delete(Long id) {
		SysUserRoles model = new SysUserRoles();
		model.setId(id);
		super.delete(model);
		return model;
	}

	@Override
	public void deleteByUserAccount(String account){
		if(StringUtils.isBlank(account)){
			return;
		}

		List<SysUserRoles> userRolesList = this.getByUserAccount(account);
		userRolesList.stream().forEach(u ->this.delete(u));
	}

	@Override
	public Long add(SysUserRoles model) {
		Long id = LongId.get();
		model.setId(id);
		super.save(model);
		return id;
	}

	@Override
	public boolean isOperationalManager(String account) {
		// TODO Auto-generated method stub

		Boolean result = false;

		Parameter parm = paraService.getByName(ConstantUser.USER_ROLE_OPERA);

		Long opearRoleId = null;
		if (parm != null) {
			String opearRoleIdStr = parm.getValue01();
			if (opearRoleIdStr != null) {
				opearRoleId =   Long.valueOf(opearRoleIdStr);
			}
		}

		if (opearRoleId != null) {
			HqlFilter hqlFilter = new HqlFilter();
			hqlFilter.addEQFilter("Account", account);
			List<SysUserRoles> userRoleList = findByFilter(hqlFilter);
			for (SysUserRoles role : userRoleList) {
				if (role.getRoleId().equals(opearRoleId)) {
					result = true;
					return result;
				}
			}
		}

		logger.info("check isOperationalManager account={}", account);
		return result;
	}

	@Override
	public boolean isTester(String account) {
		// TODO Auto-generated method stub
		Boolean result = false;

		Parameter parm = paraService.getByName(ConstantUser.USER_ROLE_TESTER);

		Long testRoleId = null;
		if (parm != null) {
			String testerRoleIdStr = parm.getValue01();
			if (testerRoleIdStr != null) {
				testRoleId =   Long.valueOf(testerRoleIdStr);
			}
		}

		if (testRoleId != null) {
			HqlFilter hqlFilter = new HqlFilter();
			hqlFilter.addEQFilter("Account", account);
			List<SysUserRoles> userRoleList = findByFilter(hqlFilter);
			for (SysUserRoles role : userRoleList) {
				if (role.getRoleId().equals(testRoleId)) {
					result = true;
					return result;
				}
			}
		}

		return result;
	}

	@Override
	public boolean isTester(Long userId) {
		Boolean result = false;
		if (userId != null) {
			SysUser user = userService.findById(userId);
			if (user != null) {
				result = isTester(user.getName());
			}
		}
		return result;
	}

	@Override
	public boolean isOperationalManager(Long userId) {

		Boolean result = false;
		if (userId != null) {
			SysUser user = userService.findById(userId);

			if (user != null) {
				result = isOperationalManager(user.getName());
			}
		}

		logger.info("check is opera userId={}, result ={}", userId, result);
		return result;
	}

	@Override
	public Boolean isChannelRegis(Long userId) {
		// TODO Auto-generated method stub
		Boolean result = false;
		SysUser user = null;
		if (userId != null) {
			user = userService.findById(userId);
		}
		
		if(user == null){
			return false;
		}
		Parameter parm = paraService.getByName(ConstantUser.USER_CHANNEL_REGIS);

		Long channelRegisRoleId = null;
		if (parm != null) {
			String channelRegisRoleIdStr = parm.getValue01();
			if (StringHelper.isNotEmpty(channelRegisRoleIdStr)) {
				channelRegisRoleId = Long.valueOf(channelRegisRoleIdStr);
			}
		}

		if (channelRegisRoleId != null) {
			HqlFilter hqlFilter = new HqlFilter();
			hqlFilter.addEQFilter("Account", user.getName());
			List<SysUserRoles> userRoleList = findByFilter(hqlFilter);
			for (SysUserRoles role : userRoleList) {
				if (role.getRoleId().equals(channelRegisRoleId)) {
					result = true;
					return result;
				}
			}
		}

		return result;
	}

	@Override
	public Boolean isDepLeader(Long userId) {

		String sql = " select count(*) from SysUser t left join SysUserRoles t1 on t.name = t1.Account    left join SysRole t2 on t1.RoleId = t2.id where t2.roleType = 2  and t.id = "+ userId ;
		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return (size == null || size == 0)  ? false : true;
	}

	@Override
	public Boolean isSafety(Long userId) {

		Boolean result = false;

		if(userId == null){
			return  false;
		}

		SysUser user = userService.findById(userId);

		if(user == null){
			return null;
		}

		Parameter parm = paraService.getByName(ConstantUser.USER_ROLE_SAFETY);

		String sql = " select count(*) from SysUser t left join SysUserRoles t1 on t.name = t1.Account    left join SysRole t2 on t1.RoleId = t2.id where t.id = "+ userId ;
		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return (size == null || size == 0)  ? false : true;
	}


	@Override
	public Boolean isTeamLeader(Long userId) {

		if(userId == null){
			return  false;
		}

		SysUser user = userService.findById(userId);

		if(user == null){
			return null;
		}

		Parameter parm = paraService.getByName(ConstantUser.USER_ROLE_TEAM_LEADER);

		Long roldId = Long.valueOf(parm.getValue01());

		String sql = String.format(" select count(*) from SysUser t where id = %d and name in (select account from  SysUserRoles  where roleId = %d ) ", userId, roldId) ;

		logger.info("sql = {}", sql);

		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return (size == null || size == 0)  ? false : true;
	}

	@Override
	public Boolean isAuth(Long userId) {

		if(userId == null){
			return  false;
		}

		SysUser user = userService.findById(userId);

		if(user == null){
			return false;
		}

		Parameter parm = paraService.getByName(ConstantUser.USER_ROLE_AUTH);

		Long roldId = Long.valueOf(parm.getValue01());

		String sql = String.format(" select count(*) from SysUser t where id = %d and name in (select account from  SysUserRoles  where roleId = %d ) ", userId, roldId) ;

		logger.info("sql = {}", sql);

		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		return (size == null || size == 0)  ? false : true;
	}




}
